package com.cams.backend.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.cams.backend.common.lang.Result;
import com.cams.backend.entity.Work;
import com.cams.backend.mapper.WorkMapper;
import com.cams.backend.service.WorkService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author cams
 * @since 2022-05-15
 */
@Service
public class WorkServiceImpl extends ServiceImpl<WorkMapper, Work> implements WorkService {

    @Resource
    WorkMapper workMapper;

    @Override
    public Result workShedule(Work work) {

        Work temp1 = workMapper.selectOne(new QueryWrapper<Work>()
                        .nested(i->i.between("wsdate",work.getWsdate(),work.getWedate()).or().between("wedate",work.getWsdate(),work.getWedate()))
                        .and(i->i.eq("uid",work.getUid()))
                );
        Work temp2 = workMapper.selectOne(new QueryWrapper<Work>()
                .nested(i->i.eq("uid",work.getUid())
                        .le("wsdate",work.getWsdate())
                        .ge("wedate",work.getWedate()))

        );

        if(temp1!=null || temp2!=null){
            return Result.fail("该用户该时间段内已被排班！");
        }
        else{
            workMapper.insert(work);
            return Result.succeed(work);
        }

    }

    @Override
    public Result workCheckAll() {
        List<Work> workList = workMapper.selectList(null);
        return Result.succeed(workList);
    }

    @Override
    public Result workCheckByUid(int uid) {
        QueryWrapper<Work> queryWrapper = new QueryWrapper<>();
        queryWrapper
                .eq("uid", uid);
        List<Work> workList = workMapper.selectList(queryWrapper);
        return Result.succeed(workList);
    }

    @Override
    public Result deleteWorkByWid(int wid) {
        workMapper.delete(new QueryWrapper<Work>().eq("wid", wid));
        return Result.succeed(null);
    }

    @Override
    public Result updateWorkInfo(Work work) {
        workMapper.updateById(work);
        return Result.succeed(work);
    }
}
